package com.zbkj.crmeb.merchant.admin.controller;

import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.merchant.admin.service.SupplierStoreOrderService;
import com.zbkj.crmeb.store.request.StoreOrderRefundRequest;
import com.zbkj.crmeb.store.request.StoreOrderSearchRequest;
import com.zbkj.crmeb.store.request.StoreOrderSendRequest;
import com.zbkj.crmeb.store.response.SupplierOrderResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * 订单表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/supplier/store/order")
@Api(tags = "供应商-订单")
public class SupplierStoreOrderController extends AdminBaseController {

    @Autowired
    private SupplierStoreOrderService supplierStoreOrderService;

    @ApiOperation(value = "订单列表")
    @GetMapping(value = "/list")
    public CommonResult<SupplierOrderResponse> list(@Validated StoreOrderSearchRequest request,
                                                    @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest){
        request.setMerId(getMerId(servletRequest));
        return CommonResult.success(supplierStoreOrderService.orderList(request, pageParamRequest));
    }

    @ApiOperation(value = "确认订单")
    @PutMapping(value = "/confirm")
    public CommonResult<Object> confirmChildOrder(@ApiParam(value = "采购订单id", required = true) @RequestParam(value = "id") Integer id,
                                                  @ApiParam(value = "false取消  true确认") @RequestParam(value = "status") Boolean status,
                                                  @ApiParam(value = "取消订单理由") @RequestParam(value = "remark") String remark,
                                                  HttpServletRequest servletRequest){
        return supplierStoreOrderService.confirmOrder(id,status,remark,getMerId(servletRequest),CrmebUtil.getClientIp(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "发送货")
    @PostMapping(value = "/send")
    public CommonResult<Boolean> send(@RequestBody @Validated StoreOrderSendRequest request){
        if(!request.getType().equals("3")){
            if(StringUtils.isBlank(request.getExpressId())){
                throw new CrmebException("请选择快递公司/填写收货人");
            }

            if(StringUtils.isBlank(request.getExpressCode())){
                throw new CrmebException("请填写快递单号/收货人手机号码");
            }
        }
        request.setChannel(3);
        return CommonResult.success(supplierStoreOrderService.send(request));
    }

    @ApiOperation(value = "拒绝退款")
    @PutMapping(value = "/refund/refuse")
    public CommonResult<Boolean> refundRefuse(@ApiParam(value = "采购订单id", required = true)@RequestParam Integer id,HttpServletRequest servletRequest){
        return CommonResult.success(supplierStoreOrderService.refundRefuse(id,getMerId(servletRequest)));
    }

    @ApiOperation(value = "退款")
    @PutMapping(value = "/refund")
    public CommonResult<Boolean> send(@RequestBody @Validated StoreOrderRefundRequest request,HttpServletRequest servletRequest){
        return CommonResult.success(supplierStoreOrderService.refund(request,getMerId(servletRequest),CrmebUtil.getClientIp(servletRequest)));
    }

}



